package com.unei.scala.demo

import org.junit.Test
import scala.io.Source

class FileDemo {

//    @Test
    def testReadFile() {
        val file = "D:/phone.txt";
        for (line <- Source.fromFile(file).getLines)
            println(line.length + ":" + line);
    }

    @Test
    def testReadFileAndFormat() {
        val file = "D:/phone.txt";
        val lines = Source.fromFile(file).getLines().toList;
        var maxWidth = 0;
        for (line <- lines) {
        	maxWidth=maxWidth.max(widthOfLength(line));
        }
        
        var longestLine=lines.reduceLeft((a,b)=>if (a.length>b.length) a else b);
        maxWidth=widthOfLength(longestLine);
        for(line<-lines){
            val numSpaces=maxWidth-widthOfLength(line);
            var padding=" "*numSpaces;
            println(padding+line.length()+"|"+line);
        }
    }

    def widthOfLength(s: String) = s.length().toString.length;
}